﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace InversPong.Model
{
    public class Game
    {
        public enum Status
        {
            Stop,
            Running,
            GameOver
        }

        public static int initialLifesCount;

        public Player Player1 { get; private set; }

        public Player Player2 { get; private set; }

        public Player[] GetPlayers()
        { 
            return new Player[2] { Player1, Player2 };
        }

        public Map Map { get; private set; }

        public Status GameStatus { get; set; }

        public Level Level { get; set; }

        private void CreateBats()
        {
            Random r = new Random();
            Vector2 player1Position = new Vector2(10, r.Next(Map.Height - Bat.defaultBatSize[1]));
            Vector2 player2Position = new Vector2(Map.Width - Bat.defaultBatSize[0] - 10, r.Next(Map.Height - Bat.defaultBatSize[1]));

            Bat bat1 = new Bat(player1Position, "bat1");
            Bat bat2 = new Bat(player2Position, "bat2");

            Player1.Bat = bat1;
            Player2.Bat = bat2;

            Map.addBat(bat1);
            Map.addBat(bat2);
        }

        private void ResetPlayers()
        {
            Player[] players = GetPlayers();
            foreach(Player player in players)
            {
                player.Bat.Position = player.Bat.InitialPosition;
                player.Bat.RemoveAllPowerUps();
                player.Lives = player.InitialLives;
            }
        }

        public Game(Status gameStatus, Level level, Player player1, Player player2, Map map)
        {
            Player1 = player1;
            Player2 = player2;
            Map = map;
            GameStatus = gameStatus;
            Level = level;
            CreateBats();
        }

        public void Reset()
        {
            Map.removeAll();
            ResetPlayers();
            GameStatus = Status.Running;
        }

    }
}
